﻿<!DOCTYPE html>  
<html lang="zh">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>短线王数据</title>  
    <style>  
        body {  
            font-family: Arial, sans-serif;  
            background-color: #000;  
            color: #BEC4CD;  
            font-size: 1vw;  
            padding: 5px;  
            box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);  
        }  

        table {  
            table-layout: fixed;  
            width: 100%;  
            border-collapse: collapse;  
            margin-top: -10px;  
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);  
        }  

        td {  
            overflow: hidden;  
            text-overflow: ellipsis;  
            max-width: 80px;  
            padding: 2px;  
        }  

        th, td {  
            border: 1px solid #444;  
            text-align: center;  
            cursor: pointer;  
            white-space: nowrap;  
            font-size: 11px;  
            background-color: rgba(255, 255, 255, 0.1);  
        }  

        th {  
            background-color: #222;  
            color: #ffffff;  
            font-weight: bold;  
            padding: 5px;  
            border: 1px solid #444;  
        }  

        th:hover {  
            background-color: #555;  
            transform: scale(1.05);  
        }  

        th[data-column] {  
            cursor: pointer;  
        }  

        tr:hover {  
            background-color: rgba(255, 255, 255, 0.2);  
        }  

        .summary {  
            margin-bottom: 0px;  
            padding: 1px;  
            border: 1px solid #444;  
            background-color: rgba(30, 30, 30, 0.7);  
            white-space: nowrap;  
            font-size: inherit;  
            border-radius: 5px;  
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);  
            font-size: 1rem;  
        }  

        .filter-container {  
            margin-bottom: 5px;  
            white-space: nowrap;  
        }  

        .date-selector {  
            margin-bottom: 5px;  
            white-space: nowrap;  
        }  

        select, input[type="checkbox"] {  
            background-color: #444;  
            color: #ffffff;  
            border: 1px solid #888;  
            padding: 8px;  
            margin-right: 8px;  
            border-radius: 5px;  
            font-size: 0.8rem;  
        }  

        select:hover, input[type="checkbox"]:hover {  
            background-color: #555;  
        }  

        button {  
            background-color: #444;  
            color: #ffffff;  
            border: none;  
            padding: 10px 15px;  
            cursor: pointer;  
            border-radius: 5px;  
            font-size: 0.8rem;  
            margin: 5px;  
        }  

        button:hover {  
            background-color: #555;  
            transform: scale(1.05);  
        }  

        ::-webkit-scrollbar {  
            height: 0rem;  
            width: 6px;  
        }  

        body::-webkit-scrollbar {  
            width: 6px;  
        }  

        body::-webkit-scrollbar-track {  
            background-color: #121212;  
        }  

        body::-webkit-scrollbar-thumb {  
            background-color: #3a3a3a;  
            border-radius: 10px;  
        }  

        body::-webkit-scrollbar-thumb:hover {  
            background-color: #ddd;  
        }  
    </style>  
</head>  
<body>  

    <div class="date-selector">  
        <label for="dateSelect">选择历史日期：</label>  
        <select id="dateSelect">  
            <option value="">请选择日期</option>  
        </select>  
    </div>  
    <div class="filter-container">  
        <label>  
            <input type="checkbox" id="filterST"> 过滤ST  
        </label>  
    </div>  
    <div class="summary" id="summary"></div>  

    <div class="button-container" style="margin-bottom: 10px;">  
        <button onclick="fetchData(lastSelectedDate, 7)">涨停抢筹</button>  
        <button onclick="fetchData(lastSelectedDate, 1)">涨停板</button>  
        <button onclick="fetchData(lastSelectedDate, 2)">曾涨停</button>  
        <button onclick="fetchData(lastSelectedDate, 3)">跌停板</button>  
        <button onclick="fetchData(lastSelectedDate, 4)">曾跌停</button>  
        <button onclick="fetchData(lastSelectedDate, 5)">潜力板</button>  
        <button onclick="fetchData(lastSelectedDate, 6)">强势股</button>  
        <button onclick="fetchData(lastSelectedDate, 8)">涨停试盘</button>  
    </div>  

    <table id="stockTable">  
        <thead>  
            <tr id="tableHeader"></tr>  
        </thead>  
        <tbody></tbody>  
    </table>  

    <script>  
        let isSTFiltered = false;  
        let isInitialLoad = true;  
        let lastSelectedDate = '';  
        let currentType = 1;  
        let currentSortOrder = 'asc';  

        async function fetchData(day, type) {  
            currentType = type;  
            try {  
                const url = `https://api.duishu.com/lhbapp/zhangting/index?pagecount=90&page=1&type=${currentType}${day ? `&day=${day}` : ''}`;  
                const response = await fetch(url);  

                if (!response.ok) {  
                    throw new Error(`HTTP error! status: ${response.status}`);  
                }  

                const data = await response.json();  

                if (data && data.data && data.data.date_list) {  
                    const dateSelect = document.getElementById('dateSelect');  
                    const currentDate = dateSelect.value;  

                    if (day !== lastSelectedDate || isInitialLoad) {  
                        dateSelect.innerHTML = '<option value="">请选择日期</option>';  
                        data.data.date_list.forEach(date => {  
                            dateSelect.innerHTML += `<option value="${date}">${date}</option>`;  
                        });  

                        if (currentDate) {  
                            dateSelect.value = currentDate;  
                            lastSelectedDate = currentDate;  
                        } else if (isInitialLoad) {  
                            dateSelect.value = data.data.date_list[0];  
                            lastSelectedDate = data.data.date_list[0];  
                            isInitialLoad = false;  
                        }  
                    }  
                }  

                if (data && data.data && data.data.sum_list) {  
                    const summaryDiv = document.getElementById('summary');  
                    let summaryHtml = '<ul>';  
                    data.data.sum_list.forEach(item => {  
                        const n1Value = typeof item.n1 === 'string' ? parseFloat(item.n1.replace('%', '').trim()) : item.n1;  
                        const n2Value = typeof item.n2 === 'string' ? parseFloat(item.n2.replace('%', '').trim()) : item.n2;  
                        const n1Color = n1Value > n2Value ? 'red' : 'green';  
                        const pValue = item.p ? parseFloat(item.p.replace('%', '').trim()) : null;  
                        const pColor = pValue !== null && pValue >= 0 ? 'red' : 'green';  

                        summaryHtml += `<li>${item.t}: `;  
                        if (item.n1 !== undefined && item.n1 !== null) {  
                            summaryHtml += `<span style="color: ${n1Color}">${item.n1}</span>`;  
                        }  
                        if (item.n2 !== undefined && item.n2 !== null) {  
                            summaryHtml += `/${item.n2}`;  
                        }  
                        if (item.p) {  
                            summaryHtml += ` <span style="color: ${pColor}">${item.p}</span>`;  
                        }  
                        summaryHtml += '</li>';  
                    });  
                    summaryHtml += '</ul>';  
                    summaryDiv.innerHTML = summaryHtml;  
                }  

                if (data && data.data && data.data.stock_list && Array.isArray(data.data.stock_list.list)) {  
                    const stockList = data.data.stock_list.list;  
                    const tbody = document.querySelector('#stockTable tbody');  
                    const tableHeader = document.getElementById('tableHeader');  
                    tbody.innerHTML = '';  
                    tableHeader.innerHTML = '';  

                    const headers = `  
                        <th data-column="0">股票代码</th>  
                        <th data-column="1">股票名称</th>  
                        ${data.data.stock_list.head_info.slice(1).map((item, index) => `<th data-column="${index + 2}">${item.text}</th>`).join('')}  
                    `;  
                    tableHeader.innerHTML = headers;  

                    tableHeader.querySelectorAll('th').forEach(th => {  
                        th.addEventListener('click', () => {  
                            const columnIndex = th.getAttribute('data-column');  
                            sortTable(columnIndex);  
                        });  
                    });  

                    stockList.forEach(item => {  
                        const row = document.createElement('tr');  
                        const stockInfo = item[0].text.split('#');  
                        const stockName = stockInfo[0].trim();  
                        const stockCode = stockInfo.length > 1 ? stockInfo[1].trim() : '';  

                        row.innerHTML = `  
                            <td>${stockCode}</td>  
                            <td>${stockName}</td>  
                            ${item.slice(1).map((cell, i) => `<td>${cell.text}</td>`).join('')}  
                        `;  

                        row.addEventListener('click', () => {  
                            window.location.href = `http://www.treeid/code_${stockCode}`;  
                            let themeColumnIndex = 4;  

                            if (currentType === 7) {  
                                themeColumnIndex = 9;  
                            } else if (currentType === 1 || currentType === 6 || currentType === 8) {  
                                themeColumnIndex = 5;  
                            }  

                            const themeValue = row.cells[themeColumnIndex].textContent.trim();  
                            highlightMatchingRows(themeColumnIndex, themeValue);  
                        });  
                        tbody.appendChild(row);  
                    });  

                    hideColumns('0#16'); 

                    if (isSTFiltered) {  
                        filterST();  
                    }  
                    updateColors();  

                } else {  
                    console.error('Error: stock_list is undefined or does not have the expected structure');  
                }  
            } catch (error) {  
                console.error('Fetch error:', error);  
            }  
        }  

        function filterST() {  
            const rows = document.querySelectorAll('#stockTable tbody tr');  
            rows.forEach(row => {  
                const stockNameCell = row.cells[1].textContent;  
                if (isSTFiltered && stockNameCell.includes('ST')) {  
                    row.style.display = 'none';  
                } else {  
                    row.style.display = '';  
                }  
            });  
        }  

        document.getElementById('filterST').addEventListener('change', (event) => {  
            isSTFiltered = event.target.checked;  
            filterST();  
        });  

        function parseValue(value) {  
            let number = parseFloat(value.replace(/[亿万%,]/g, ''));  
            if (value.includes('亿')) {  
                number *= 100000000;  
            } else if (value.includes('万')) {  
                number *= 10000;  
            }  
            return number;  
        }  

        function updateColors() {  
            const rows = document.querySelectorAll('#stockTable tbody tr');  
            rows.forEach(row => {  
                let financialIndices = [];  
                let percentIndices = [];  

                switch (currentType) {  
                    case 7:  
                        percentIndices = [3, 4, 15];  
                        break;  
                    case 1:  
                        financialIndices = [9];  
                        percentIndices = [17];  
                        break;  
                    case 2:  
                        financialIndices = [7];  
                        percentIndices = [2, 14];  
                        break;  
                    case 3:  
                        financialIndices = [8];  
                        percentIndices = [16];  
                        break;  
                    case 4:  
                        financialIndices = [7];  
                        percentIndices = [2, 13];  
                        break;  
                    case 5:  
                        financialIndices = [3, 8];  
                        percentIndices = [2, 5, 12];  
                        break;  
                    case 6:  
                        financialIndices = [13, 14];  
                        percentIndices = [2, 15, 16];  
                        break;  
                    case 8:  
                        financialIndices = [17];  
                        percentIndices = [9];  
                        break;  
                }  

                financialIndices.forEach(index => {  
                    const cell = row.cells[index];  
                    if (cell) {  
                        const value = parseValue(cell.textContent);  
                        cell.style.color = value >= 0 ? 'red' : 'green';  
                    }  
                });  

                percentIndices.forEach(index => {  
                    const cell = row.cells[index];  
                    if (cell) {  
                        const value = parsePercent(cell.textContent);  
                        cell.style.color = value >= 0 ? 'red' : 'green';  
                    }  
                });  
            });  
        }  

        function parsePercent(value) {  
            return parseFloat(value.replace('%', '').trim());  
        }  

        function parseNumeric(value) {  
            return parseFloat(value.replace(/,/g, '').trim());  
        }  

        function parseInteger(value) {  
            return parseInt(value.replace(/,/g, '').trim(), 10);  
        }  

        function parseTime(value) {  
            const [hours, minutes] = value.split(':').map(Number);  
            return hours * 60 + minutes;  
        }  

        function parseLeadingNumber(value) {  
            if (value.startsWith("首板")) {  
                return 0;  
            }  
            const match = value.match(/^(\d+)/);  
            return match ? parseInt(match[1], 10) : 0;  
        }  

        function sortTable(columnIndex) {  
            const tbody = document.querySelector('#stockTable tbody');  
            const rows = Array.from(tbody.rows);  

            rows.sort((rowA, rowB) => {  
                const cellA = rowA.cells[columnIndex].textContent.trim();  
                const cellB = rowB.cells[columnIndex].textContent.trim();  

                let valueA, valueB;  

                if (cellA.includes('亿') || cellA.includes('万')) {  
                    valueA = parseValue(cellA);  
                    valueB = parseValue(cellB);  
                } else if (cellA.includes('.') || /^\d+$/.test(cellA)) {  
                    valueA = parseNumeric(cellA);  
                    valueB = parseNumeric(cellB);  
                } else if (/^\d+/.test(cellA) || cellA.startsWith("首板")) {  
                    valueA = parseLeadingNumber(cellA);  
                    valueB = parseLeadingNumber(cellB);  
                } else if (/^\d{2}:\d{2}/.test(cellA)) {  
                    valueA = parseTime(cellA);  
                    valueB = parseTime(cellB);  
                } else {  
                    valueA = cellA;  
                    valueB = cellB;  
                }  

                if (typeof valueA === 'string' && typeof valueB === 'string') {  
                    return currentSortOrder === 'asc' ? valueA.localeCompare(valueB) : valueB.localeCompare(valueA);  
                } else {  
                    return currentSortOrder === 'asc' ? valueA - valueB : valueB - valueA;  
                }  
            });  

            rows.forEach(row => tbody.appendChild(row));  
            currentSortOrder = currentSortOrder === 'asc' ? 'desc' : 'asc';  
            updateColors();  
        }  

        document.getElementById('dateSelect').addEventListener('change', (event) => {  
            const selectedDate = event.target.value;  
            fetchData(selectedDate, currentType);  
        });  

        window.onload = () => {  
            document.getElementById('filterST').checked = true;  
            isSTFiltered = true;
            fetchData(0, currentType);  
        };  

        function highlightMatchingRows(columnIndex, targetValue) {  
            const rows = document.querySelectorAll('#stockTable tbody tr');  
            rows.forEach(row => {  
                row.style.backgroundColor = '';  
                row.style.color = '';  
            });  

            rows.forEach(row => {  
                const cellValue = row.cells[columnIndex].textContent.trim();  
                if (cellValue === targetValue) {  
                    row.style.backgroundColor = 'darkred';  
                    row.style.color = 'white';  
                }  
            });  
        }  


        function hideColumns(columnIndices) {  
            const indices = columnIndices.split('#').map(Number);  
            const headers = document.querySelectorAll('#stockTable th');  
            const rows = document.querySelectorAll('#stockTable tbody tr'); 

            indices.forEach(index => {  
                if (headers[index]) {  
                    headers[index].style.display = 'none';  
                }  
            });  

            rows.forEach(row => {  
                indices.forEach(index => {  
                    if (row.cells[index]) {  
                        row.cells[index].style.display = 'none';  
                    }  
                });  
            });  
        }  
    </script>  
</body>  
</html>